/**
 * @file ServerThread.cpp
 * @author Jakub Trzaskoma
 *
 * Implementacja klasy odpowiadiadającej za wysyłanie i odbieranie komunikatow pomiędzy klientami a serwerem.
 */

#include "ServerThread.h"
#include "Log.h"

namespace tftp_server
{

Package ServerThread::receivePackage(int socket_,
		struct sockaddr_in clientName_)
{
	char buffer[1024];
	socklen_t clientNameLength = sizeof(clientName_);
	if (recvfrom(socket_, buffer, 1024, 0, (struct sockaddr*) (&clientName_),
			&clientNameLength) == -1)
		Loger::getInstance().send(Log(
				"ServerThread: Fail to read package from client!", 2));

	Package package(buffer);
	return package;
}
void ServerThread::sendPackage(Package package_, int socket_,
		struct sockaddr_in clientName_)
{
	char buffer[1024];
	package_.makeBuffer(buffer);
	if (sendto(socket_, buffer, package_.getBufferSize(), 0,
			(struct sockaddr*) (&clientName_), sizeof(sockaddr_in)) == -1)
		Loger::getInstance().send(Log(
				"ServerThread: Fail to send package to client!", 2));
}

}
